# == Schema Information
# Schema version: 20090117201647
#
# Table name: users
#
#  id               :integer         not null, primary key
#  login            :string(255)
#  email            :string(255)
#  crypted_password :string(255)
#  salt             :string(255)
#  activation_token :string(255)
#  activated_at     :datetime
#  timezone         :string(255)
#  localization     :string(255)
#  created_at       :datetime
#  updated_at       :datetime
#

class User < ActiveRecord::Base
  attr_accessor :password, :password_confirmation, :updating_password 
  
  LOGIN_MIN_LENGTH = 4
  LOGIN_MAX_LENGTH = 20
  LOGIN_LENGTH_RANGE = LOGIN_MIN_LENGTH..LOGIN_MAX_LENGTH
  PASSWORD_MIN_LENGTH = 6
  
  validates_presence_of     :login, :email
  
  validates_presence_of     :password, :password_confirmation,
                            :if => :changing_password
                            
  validates_length_of       :login, 
                            :within => LOGIN_LENGTH_RANGE,
                            :allow_blank => true
                            
  validates_length_of       :password,
                            :minimum => PASSWORD_MIN_LENGTH,
                            :if => :changing_password,
                            :allow_blank => true
                            
  validates_confirmation_of :password,
                            :if => :changing_password
                            
  validates_uniqueness_of   :login, :email
  
  protected
  
  def changing_password
    new_record? || updating_password?
  end
  
  private
  
  def updating_password?
    updating_password
  end
end
